$(document).ready(function () {
	var ctx = $("#ctx").val();//获取服务器地址
	/***************************    广告主消费记录,分配记录列表页    ***************************/

	if ($('#calendar_span').html()) {
		$('#dateRangePicker').dateRangePicker();
		if ($('#dateRangePicker').val() == "") {
			$('#dateRangePicker').val(getlastDate(1) + " to " + getlastDate(1));
		}
	}

	//点击日期控件的按钮时，去掉前面时间标签的样式
	$(".apply-btn").click(function (event, obj) {
		if (!$(this).hasClass("disabled")) {
			$('#dateRangePicker').val($(".start-day").text() + " to " + $(".end-day").text());
		}
	});

	/***************************    资金分配    ***************************/

	//初始校验表单
	if ($("#validateForm").html()) {
		$("#validateForm").autovalidate();
	}

	$(".btn-save").click(function () {
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = validateMoney();
		if (falg == false) {
			return false;
		}
		$("#validateForm").submit();
		return false;
	});

	$(".btn-cancel").click(function () {
		window.location.href = ctx + "/advertiser/main/finance/finance-data/list";
	});

	$("#allocate").blur(function () {
		validateMoney();
	});

	$("#btn_deposit").click(function () {
		layer.open({
			type: 2,
			title: '充值',
			shadeClose: false,
			shade: 0.6,
			fix: false,
			area: ['500px', '230px'],
			content: [ctx + '/advertiser/main/ad/target/ad-group-alloperator/deposit', 'no']
		});
	});
});

//导出广告主消费记录
function exportExcel_list() {
	var param = "?selectedAdvertiserId=" + $("#advertiserId").val() + "&dateRangePicker=" + $("#dateRangePicker").val() + "&type=" + $("#typeId").val();
	$("#btn_formExport").attr("href", $("#ctx").val() + "/advertiser/main/finance/adv-balance/exportDataReportExcel" + param);
	return false;
}

//导出广告主分配记录
function exportExcel_distribution() {
	var param = "?selectedAdvertiserId=" + $("#advertiserId").val() + "&dateRangePicker=" + $("#dateRangePicker").val();
	$("#btn_formExport").attr("href", $("#ctx").val() + "/advertiser/main/finance/adv-balance/exportAllocateReportExcel" + param);
	return false;
}

//导出资金分配
function exportExcel_financelist() {
	var param = "?selectedAdvertiserId=" + $("#advertiserId").val();
	$("#btn_formExport").attr("href", $("#ctx").val() + "/advertiser/main/finance/finance-data/exportDataReportExcel" + param);
	return false;
}

function validateMoney() {
	var reg = /^\d{1,8}\.?\d{0,2}$/;
	var $this = $("#allocate");
	if ($this.val().match(reg) != null) {
		if (($this.val().indexOf('.') == -1 && $this.val().length > 8) || $this.val() <= 0) {
			$this.css("box-shadow", "0 0 6px #ff0000").attr('title', '分配金额必须大于0且整数位最多8位');
			$this.parent("div").find("span.spanVaildate").html("分配金额必须大于0且整数位最多8位");
			return false;
		} else if ($this.val() > parseInt($("#agentBalance").val())) {
			$this.css("box-shadow", "0 0 6px #ff0000").attr('title', '分配金额不能大于当前余额');
			$this.parent("div").find("span.spanVaildate").html("分配金额不能大于当前余额");
			return false;
		} else {
			$this.css("box-shadow", "none").attr('title', '');
			$this.parent("div").find("span.spanVaildate").html("");
		}
	} else {
		$this.css("box-shadow", "0 0 6px #ff0000").attr('title', '分配金额必须大于0整数位最多8位且最多有2位小数');
		$this.parent("div").find("span.spanVaildate").html("分配金额必须大于0整数位最多8位且最多有2位小数");
		return false;
	}
}